/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Shared;

import DataManager.DbManager;
import DataManager.MySQLCommonDataProvider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *
 * @author Apolloz
 */

public class MapManager {

    public static Logger log = LoggerFactory.getLogger(MapManager.class.getName());

    private static Map<Integer, GameMapObj> maps = Collections.synchronizedMap(new HashMap<Integer, GameMapObj>());
    
    private static MapManager mapManager = null;

    public synchronized static MapManager getInstance() {
       if(mapManager == null) {
          mapManager = new MapManager();
       }
       return mapManager;
    }
    
    public MapManager() {
        initGameMaps();
    }

    public GameMapObj getMap(int mapID) {
        return maps.get(mapID);
    }
    
    private void initGameMaps() {
        MySQLCommonDataProvider dataManager = DbManager.GetCommonDataManager();
        ArrayList mapList = dataManager.getMapList();
        int noOfMaps = mapList.size();
        for (int i = 0; i < noOfMaps; i++) {
            GameMapObj gm = (GameMapObj)mapList.get(i);
            int startId = gm.mapID;
            int numOfZones = gm.noOfZones;
            String name = gm.name;
            for (int zoneIndex = 0; zoneIndex < numOfZones; zoneIndex++){
                if (zoneIndex != 0) {
                    gm = new GameMapObj(startId + zoneIndex, name);
                }
                maps.put(gm.mapID, gm);
            }
        }
        log.info("Map numbers=" + MapManager.maps.size());

    }

    public String getMapCodeFromID(String mapID) {
        return mapID.substring(0,4);
    }
}
